﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.IO;

namespace _4T
{
    [ServiceContract(Namespace = "http://tempuri.org/")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ServiceAjax : System.Web.UI.Page
    {
        [OperationContract]
        public string SaveComment(string body, string profileCode, int stage)
        {
            int result = 0;
            string personalCode = Session["User"].ToString();
            if (body != null && body != "")
                result = new DAL.CommentDAL().InsertComment(personalCode, profileCode, stage, body);
            if (result > 0)
                return "successful";
            else
                return "faile";
        }

        [OperationContract]
        public string UploadFile(object obj)
        {
            var length = Request.ContentLength;
            var bytes = new byte[length];
            Request.InputStream.Read(bytes, 0, length);
            // bytes has byte content here. what do do next?

            var fileName = Request.Headers["X-File-Name"];
            var fileSize = Request.Headers["X-File-Size"];
            var fileType = Request.Headers["X-File-Type"];

            var saveToFileLoc = string.Format("{0}\\{1}",
                                           Server.MapPath("/Files"),
                                           fileName);

            // save the file.
            var fileStream = new FileStream(saveToFileLoc, FileMode.Create, FileAccess.ReadWrite);
            fileStream.Write(bytes, 0, length);
            fileStream.Close();

            return string.Format("{0} bytes uploaded", bytes.Length);
        }
    }
}
